package com.qding.park.common.core.aspect.handle;

import java.lang.reflect.Method;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.MethodSignature;

import com.qding.park.common.core.annotation.MethodCachEvict;
import com.qding.park.common.core.aspect.ExpContextHelper;
import com.qding.park.common.core.aspect.context.CacheEvictContext;
import com.qding.park.common.core.redis.JedisClient;

/**
  * @ClassName: MethodCachEvictHandler
  * @Description: MethodCachEvict  注解处理
  * @author Comsys-Administrator
  * @date 2016年11月10日 下午2:14:15
  *
  */
public class MethodCachEvictHandler extends AbstractCacheHandle{

	private final static Log _log = LogFactory.getLog(MethodCachEvictHandler.class);
	
	
	/**
	 * @Field @methodCacheable : TODO(MethodCacheable 注解)
	 */
	private MethodCachEvict methodCachEvict;

	/**
	 * @Field @pjp : TODO(MethodCacheable 注解的切面 )
	 */
	private ProceedingJoinPoint pjp;
	
	
	
	/**
	 * @throws Throwable 
	 * @Description (执行 缓存业务)
	 */
	public Object doCache( MethodCachEvict cachEvict, ProceedingJoinPoint pjpoint,JedisClient jedisClient) throws Throwable {
		
		
		//得到上下文环境
		CacheEvictContext context=ExpContextHelper.createCacheContext( cachEvict,  pjpoint);
		
		return null;
//		if (cacheSwitch == MethodCacheable.CACHESWITCH.OFF) {
//			// 关闭了缓存
//			Object result = pjpoint.proceed(pjpoint.getArgs());
//			return result;
//		} else {
//			// 开启缓存
//			
//			// 业务类型
//			MethodCacheable.SERVICETYPE serviceType = cacheable.serviceType();
//
//			int expire=context.getExpire();
//			
//			
//			if (serviceType == MethodCacheable.SERVICETYPE.NULL) {
//				// 不执行业务方法
//				Object returnObj = context.getCache_value();
//				return SerializeUtils.unSerialize((String)returnObj, context.getReturnType());
//			} else if (serviceType == MethodCacheable.SERVICETYPE.BEFORE) {
//				// 在业务方法之前执行
//				Object returnObj = context.getCache_value();// 从缓存之中获取
//				returnObj=SerializeUtils.unSerialize((String)returnObj, context.getReturnType());
//				if (returnObj == null) {
//					// 如果缓存没有
//					returnObj = pjpoint.proceed(pjpoint.getArgs());
//					this.saveCache( context,  jedisClient, SerializeUtils.serialize(returnObj));
//				}
//				return returnObj;
//
//			} else if (serviceType == MethodCacheable.SERVICETYPE.AFTER) {
//				// 在业务方法之后执行
//				Object returnObj = pjpoint.proceed(pjpoint.getArgs());
//				if(returnObj!=null){
//					this.saveCache( context,  jedisClient, SerializeUtils.serialize(returnObj));
//				}
//				return returnObj;
//			} else {
//				return pjpoint.proceed(pjpoint.getArgs());
//			}
//
//		}
	}

	
	
//	public Object doCache(JedisClient jedisClient) throws Throwable {
//		return doCache(methodCacheable,pjp,jedisClient);
//	}

//	public MethodCacheable getMethodCacheable() {
//		return methodCacheable;
//	}
	


	private Method getMethod(ProceedingJoinPoint pjp) {
		MethodSignature signature = (MethodSignature) pjp.getSignature();
		Method method = signature.getMethod();

		return method;

	}
//	public void setMethodCacheable(MethodCacheable methodCacheable) {
//		this.methodCacheable = methodCacheable;
//	}

	public ProceedingJoinPoint getPjp() {
		return pjp;
	}

	public void setPjp(ProceedingJoinPoint pjp) {
		this.pjp = pjp;
	}
}
